﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NicoView.Model.Service
{
    /// <summary>
    /// ニコニコサービスプロバイダ群
    /// </summary>
    public sealed class NicoServiceProviders
    {
        public Dictionary<Guid, NicoServiceProvider> Services { get; private set; }



        public void Set(NicoServiceProvider serviceProvider)
        {
            this.Set(serviceProvider.Auth.Auth);
        }

        private void Set(NicoSafeUserAuth auth)
        {
            Services[auth.IdentificationKey] = new NicoServiceProvider{ Auth = new NotificationNicoAuth { Auth = auth} };
        }

        public void Remove(NicoServiceProvider service)
        {
            if (service.Auth == null) return;
            Services.Remove(service.Auth.IdentificationKey);
        }

        public int Count
        {
            get
            {
                return this.Services.Count;
            }
        }


        /// <summary>
        /// サービスリスト内の認証情報をリストで取得
        /// </summary>
        /// <returns></returns>
        public NicoUserAuthList GetAuth()
        {
            var list = new NicoUserAuthList();

            foreach (var service in this.Services)
            {
                list.AuthList.Add(service.Value.Auth.Auth);
            }
            return list;
        }

        /// <summary>
        /// コンストラクタ
        /// </summary>
        /// <param name="authList"></param>
        internal NicoServiceProviders(ICollection<NicoSafeUserAuth> authList)
        {
            Services = new Dictionary<Guid, NicoServiceProvider>();
            foreach (var auth in authList)
            {

                if (auth.IsEnableAuth()) this.Set(auth);
            }
        }
    }
}
